package org.yibinxueyuan.drawcard.controller;

import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yibinxueyuan.drawcard.entity.Cardinfo;
import org.yibinxueyuan.drawcard.pojo.Paging;
import org.yibinxueyuan.drawcard.service.CardInfoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 卡片管理页面的控制
 */
@Controller
public class CardController extends ApplicationObjectSupport {
    @Resource(name = "CardinfoServiceImpl")
    private CardInfoService cardInfoService;

    @Resource(name = "pagingBean")
    private Paging pag;


    @RequestMapping(value = "/cardmanage")
    public String cardManage(Paging paging, Model m, HttpServletRequest request){
        List<Cardinfo> cardinfos = null;
        int number = -1;
        //设置message提示信息

        String message = request.getParameter("message");
        m.addAttribute("message",message);

        //设置如何进行分页查询,如果有paging分页信息传入,则按传入信息分页
        //如果没有paging信息传入,则按照默认分页方式分页
        if(paging != null &&paging.getFirst() != -1){
            number = cardInfoService.queryAllTotalCardNumber(paging.getCardName());
            paging.setTotalNumber(number);
            cardinfos = cardInfoService.queryAllCardByPage(paging);
            m.addAttribute("cardinfos",cardinfos);
            m.addAttribute("paging",paging);
            return "manage/card";
        }else{
            paging = (Paging) getApplicationContext().getBean("pagingBean");
            cardinfos = cardInfoService.queryAllCardByPage(paging);
            number = cardInfoService.queryAllTotalCardNumber(paging.getCardName());
            paging.setTotalNumber(number);
            m.addAttribute("cardinfos",cardinfos);
            m.addAttribute("paging",paging);
            return "manage/card";
        }
    }


}
